package com.mzpai.web.controller;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.mzpai.product.exception.ProductResExceptionCode;
import com.mzpai.product.model.StoreUserRemark;
import com.mzpai.product.service.StoreUserRemarkService;
import com.mzpai.product.vo.UpdateStoreUserRemarkVo;
import com.mzpai.util.StringUtils;
import com.mzpai.vo.ResponseVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(value = "用户门店备注接口")
@RestController
@RequestMapping("/storeUserRemark")
public class StoreUserRemarkController extends BaseController {

	private static Logger log = LoggerFactory.getLogger(StoreUserRemarkController.class);
	
	@Autowired 
	private StoreUserRemarkService storeUserRemarkService;
	
	@ApiOperation(value = "查询用户在门店备注信息")
	@RequestMapping(value = "/queryStoreUserRemark",method = {RequestMethod. POST ,RequestMethod. GET })
	public ResponseVo<StoreUserRemark> queryStoreUserRemark(
			@ApiParam(required = true, name = "userId", value = "入参为用户id")
			@RequestParam(value = "userId", required = true) Integer userId,
			@ApiParam(required = true, name = "storeId", value = "门店id")
			@RequestParam(value = "storeId", required = true) Integer storeId) throws Exception {
	
		logger.info("msg1=开始查询用户在门店备注信息,,method=queryStoreUserRemark,,userId={},,storeId={}",userId,storeId);
		
		if(userId==null){
			ResponseVo<StoreUserRemark> resVo=new ResponseVo<StoreUserRemark>();
			resVo.setCode(ProductResExceptionCode.USER_IDNULL_ERROR.getCode());
			resVo.setMessage(ProductResExceptionCode.USER_IDNULL_ERROR.getMessage());
			return resVo;
		}
		
		if(storeId==null){
			ResponseVo<StoreUserRemark> resVo=new ResponseVo<StoreUserRemark>();
			resVo.setCode(ProductResExceptionCode.STOREID_NULL.getCode());
			resVo.setMessage(ProductResExceptionCode.STOREID_NULL.getMessage());
			return resVo;
		}
		
		Map<String,Object> queryMap=new HashMap<String,Object>();
		queryMap.put("storeId", storeId);
		queryMap.put("userId", userId);
		List<StoreUserRemark> storeUserRemarkList=storeUserRemarkService.getList(queryMap);
		
		StoreUserRemark storeUserRemark=null;
		if(storeUserRemarkList!=null && !storeUserRemarkList.isEmpty()){
			storeUserRemark=storeUserRemarkList.get(0);
		}
		
		ResponseVo<StoreUserRemark> res = new ResponseVo<StoreUserRemark>();
		res.setResult(storeUserRemark);
		logger.info("msg1=结束查询用户在门店备注信息,,method=queryStoreUserRemark,,userId={},,storeId={}",userId,storeId);
		return res;
	}

	
	@ApiOperation(value = "门店用户备注新增或修改")
    @RequestMapping(value = "/addOrUpdateStoreUserRemark")
    public ResponseVo<Boolean> addOrUpdateStoreUserRemark(@RequestParam(value = "storeUserRemark", required = true) String storeUserRemark){
		
		log.info("msg1=门店用户备注新增,,method=addProduct,,product={}", storeUserRemark);
		
		UpdateStoreUserRemarkVo reqVo = JSONObject.parseObject(storeUserRemark, UpdateStoreUserRemarkVo.class);
	
		String operateFlage=reqVo.getOperateFlage();
		Integer id=reqVo.getId();
		Integer userId=reqVo.getUserId();
		Integer storeId=reqVo.getStoreId();
		String remark=reqVo.getRemark();
		
		if(StringUtils.isNullOrBlank(operateFlage)){
			ResponseVo<Boolean> resultVo=new ResponseVo<Boolean>();
			resultVo.setResult(false);
			resultVo.setCode(ProductResExceptionCode.ADD_UPDATE_OPERATEFLAGE_NULL.getCode());
			resultVo.setMessage(ProductResExceptionCode.ADD_UPDATE_OPERATEFLAGE_NULL.getMessage());
			return resultVo;
		}
		
		if(storeId==null){
			ResponseVo<Boolean> resultVo=new ResponseVo<Boolean>();
			resultVo.setResult(false);
			resultVo.setCode(ProductResExceptionCode.STOREID_NULL.getCode());
			resultVo.setMessage(ProductResExceptionCode.STOREID_NULL.getMessage());
			return resultVo;
		}
		
		if(userId==null){
			ResponseVo<Boolean> resultVo=new ResponseVo<Boolean>();
			resultVo.setResult(false);
			resultVo.setCode(ProductResExceptionCode.USER_IDNULL_ERROR.getCode());
			resultVo.setMessage(ProductResExceptionCode.USER_IDNULL_ERROR.getMessage());
			return resultVo;
		}
		
		if("add".equals(operateFlage)){
			StoreUserRemark saveStoreUserRemark=new StoreUserRemark();
			saveStoreUserRemark.setUserId(userId);
			saveStoreUserRemark.setStoreId(storeId);
			saveStoreUserRemark.setAddTime(new Date());
			saveStoreUserRemark.setUpdateTime(new Date());
			saveStoreUserRemark.setRemark(remark);
			storeUserRemarkService.save(saveStoreUserRemark);
		}else if("update".equals(operateFlage)){
			if(id==null){
				ResponseVo<Boolean> resultVo=new ResponseVo<Boolean>();
				resultVo.setResult(false);
				resultVo.setCode(ProductResExceptionCode.PRIMARY_ID_NULL.getCode());
				resultVo.setMessage(ProductResExceptionCode.PRIMARY_ID_NULL.getMessage());
				return resultVo;
			}
			StoreUserRemark updateStoreUserRemark=new StoreUserRemark();
			updateStoreUserRemark.setId(id);
			updateStoreUserRemark.setUserId(userId);
			updateStoreUserRemark.setStoreId(storeId);
			updateStoreUserRemark.setUpdateTime(new Date());
			updateStoreUserRemark.setRemark(remark);
			storeUserRemarkService.updateByUserAndStoreId(updateStoreUserRemark);
		}else{
			ResponseVo<Boolean> resultVo=new ResponseVo<Boolean>();
			resultVo.setResult(false);
			resultVo.setCode(ProductResExceptionCode.ADD_UPDATE_OPERATEFLAGE_ERROR.getCode());
			resultVo.setMessage(ProductResExceptionCode.ADD_UPDATE_OPERATEFLAGE_ERROR.getMessage());
			return resultVo;
		}
		
		ResponseVo<Boolean> resVo=new ResponseVo<Boolean>();
		resVo.setResult(true);
    	return resVo;
	}
	
}